package com.mlog.njyjy.ndop.msg.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.mlog.njyjy.ndop.msg.admin.service.CollectItemService;
import com.mlog.njyjy.ndop.msg.common.domain.CollectItem;
import com.mlog.njyjy.ndop.msg.common.support.DateTimeUtil;
import com.mlog.njyjy.ndop.msg.persistence.dao.CollectItemRepository;
import com.mlog.njyjy.ndop.share.sql.support.SqlTableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/11
 */
@Slf4j
@Service
public class CollectItemServiceImpl implements CollectItemService {

    @Autowired
    private CollectItemRepository collectItemRepository;

    /**
     * 同步采集项
     *
     * @param type
     * @return
     */
    @Override
    public boolean syncItems(String type) {

        //FIXME: 同步
        String jsonStr = getItemInfo(type);
        JsonElement jsonElement = new JsonParser().parse(jsonStr);
        boolean isArr = jsonElement.isJsonArray();
        if (isArr) {
            JSONArray jsonArray = new JSONArray(jsonStr);
            List<CollectItem> items = JSONUtil.toList(jsonArray, CollectItem.class);
            items = items.stream().peek(collectItem -> collectItem.setType(type)).collect(Collectors.toList());
            return collectItemRepository.insert(items);
        } else {
            CollectItem item = JSONUtil.toBean(jsonStr, CollectItem.class);
            item.setType(type);
            return collectItemRepository.insert(item);
        }
    }

    /**
     * 列出全部采集项
     *
     * @param type
     * @return
     */
    @Override
    public List<CollectItem> listItems(String type) {

        return collectItemRepository.find("select * from " + SqlTableUtil.getTblName(CollectItem.class) + " where type = ?", type);
    }

    /**
     * 启用或取消采集项
     *
     * @param type
     * @param ids
     * @param status
     * @return
     */
    @Override
    public boolean changeStatus(String type, String ids, int status) {
        boolean flag = true;
        if (StrUtil.isNotBlank(ids)) {
            String[] idArr = ids.split(",");

            for (String id : idArr) {
                CollectItem item = collectItemRepository.findFirst("select * from " + SqlTableUtil.getTblName(CollectItem.class)
                        + " where id = " + Long.valueOf(id) + " and type = '" + type + "'");
                if (item != null) {
                    item.setStatus(status);
                    boolean success = collectItemRepository.update(item);
                    if (!success) {
                        log.error("update collectItem failed, id= {}", id);
                        flag = false;
                    }
                } else {
                    log.error("can't find the collectItem, id = {}, type = {}", id, type);
                    flag = false;
                }

            }
        }
        return flag;
    }

    /**
     * 列出所有启用的采集项
     *
     * @param type
     * @return
     */
    @Override
    public List<CollectItem> listEnabledItems(String type) {
        return collectItemRepository.find("select * from " + SqlTableUtil.getTblName(CollectItem.class) + " where type = ? and status = 1", type);
    }

    private String getItemInfo(String type) {

        return "[\n" +
                "  {\n" +
                "    \"name\": \"" + type + DateTimeUtil.toTimeStamp() + "\",\n" +
                "    \"description\": \"描述\"\n" +
                "  }\n" +
                "]";
    }
}
